package com.example.Android_test.activity;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.Android_test.R;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.specs.WordSpec;

import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 10.06.13
 * Time: 13:32
 * To change this template use File | Settings | File Templates.
 */
public class WordsOfDayDialog extends Dialog {
    private final String TITLE = getContext().getString(R.string.wod_textView_Title);
    private final String DESCRIPTION = getContext().getString(R.string.wod_textView_Description);

    private TextView wod_textView_description;

    private TextView wod_textView_word1;
    private TextView wod_textView_word1_context;
    private TextView wod_textView_word1_contextTranslation;

    private TextView wod_textView_word2;
    private TextView wod_textView_word2_context;
    private TextView wod_textView_word2_contextTranslation;

    private TextView wod_textView_word3;
    private TextView wod_textView_word3_context;
    private TextView wod_textView_word3_contextTranslation;

    private Button wod_button_close;


    public WordsOfDayDialog(Context context) {
        super(context);

        this.setContentView(R.layout.other_words_of_day);
        this.setTitle(TITLE);
        this.setCancelable(true);

        wod_textView_description = (TextView) findViewById(R.id.wod_textView_description);
        wod_textView_description.setText(DESCRIPTION);

        wod_textView_word1 = (TextView) findViewById(R.id.wod_textView_word1);
        wod_textView_word1_context = (TextView) findViewById(R.id.wod_textView_word1_context);
        wod_textView_word1_contextTranslation = (TextView) findViewById(R.id.wod_textView_word1_contextTranslation);

        wod_textView_word2 = (TextView) findViewById(R.id.wod_textView_word2);
        wod_textView_word2_context = (TextView) findViewById(R.id.wod_textView_word2_context);
        wod_textView_word2_contextTranslation = (TextView) findViewById(R.id.wod_textView_word2_contextTranslation);

        wod_textView_word3 = (TextView) findViewById(R.id.wod_textView_word3);
        wod_textView_word3_context = (TextView) findViewById(R.id.wod_textView_word3_context);
        wod_textView_word3_contextTranslation = (TextView) findViewById(R.id.wod_textView_word3_contextTranslation);

        wod_button_close = (Button) findViewById(R.id.wod_button_close);
        wod_button_close.setOnClickListener(new ButtonCloseListener());

        prepareWords();
    }

    private void prepareWords() {
        HashSet<WordSpec> words = new HashSet<WordSpec>();

        while (words.size() < 3) {
            try {
                words.add(SOWEngine.INSTANCE.getWordToTraining());
            } catch (Exception e) {
                Log.e(Main.LOG_TAG, "WordsOfDayDialog.prepareWords() error " + e.getMessage());  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        int currentWordNum = 1;
        for (WordSpec word : words) {
            Log.d(Main.LOG_TAG, "word = " + word.getWord());

            String wordTitle = "<b>"+word.getWord()+"</b> - " + word.getTranslationVariantSpecs().get(0).getTranslate();
            String wordContext = "<i>"+word.getInContext()+"</i>";
            String wordContextTranslation = "<i>"+word.getInContextTranslation()+"</i>";

            switch (currentWordNum) {
                case 1:
                    wod_textView_word1.setText(Html.fromHtml(wordTitle));
                    wod_textView_word1_context.setText(Html.fromHtml(wordContext));
                    wod_textView_word1_contextTranslation.setText(Html.fromHtml(wordContextTranslation));
                    break;
                case 2:
                    wod_textView_word2.setText(Html.fromHtml(wordTitle));
                    wod_textView_word2_context.setText(Html.fromHtml(wordContext));
                    wod_textView_word2_contextTranslation.setText(Html.fromHtml(wordContextTranslation));
                    break;
                case 3:
                    wod_textView_word3.setText(Html.fromHtml(wordTitle));
                    wod_textView_word3_context.setText(Html.fromHtml(wordContext));
                    wod_textView_word3_contextTranslation.setText(Html.fromHtml(wordContextTranslation));
                    break;
            }

            currentWordNum++;
        }
    }

    private class ButtonCloseListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            cancel();
        }
    }
}